home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Clock2.java < prev    next >
Text File  |  1998-09-15  |  8KB  |  227 lines

  1. /*
  2.  * @(#)Clock2.java    1.5 98/07/13
  3.  *
  4.  * Copyright (c) 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.util.*;
  32. import java.awt.*;
  33. import java.applet.*;
  34. import java.text.*;
  35.  
  36. /**
  37.  * Time!
  38.  *
  39.  * @author Rachel Gollub
  40.  */
  41.  
  42. public class Clock2 extends Applet implements Runnable {
  43.     Thread timer;                // The thread that displays clock
  44.     int lastxs, lastys, lastxm,
  45.         lastym, lastxh, lastyh;  // Dimensions used to draw hands 
  46.     SimpleDateFormat formatter;  // Formats the date displayed
  47.     String lastdate;             // String to hold date displayed
  48.     Font clockFaceFont;          // Font for number display on clock
  49.     Date currentDate;            // Used to get date to display
  50.     Color handColor;             // Color of main hands and dial
  51.     Color numberColor;           // Color of second hand and numbers
  52.  
  53.     public void init() {
  54.         int x,y;
  55.         lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
  56.         formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
  57.         currentDate = new Date();
  58.         lastdate = formatter.format(currentDate);
  59.         clockFaceFont = new Font("Serif", Font.PLAIN, 14);
  60.         handColor = Color.blue;
  61.         numberColor = Color.darkGray;
  62.  
  63.         try {
  64.             setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
  65.         } catch (Exception E) { }
  66.         try {
  67.             handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),16));
  68.         } catch (Exception E) { }
  69.         try {
  70.             numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),16));
  71.         } catch (Exception E) { }
  72.         resize(300,300);              // Set clock window size
  73.     }
  74.  
  75.     // Plotpoints allows calculation to only cover 45 degrees of the circle,
  76.     // and then mirror
  77.     public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  78.         g.drawLine(x0+x,y0+y,x0+x,y0+y);
  79.         g.drawLine(x0+y,y0+x,x0+y,y0+x);
  80.         g.drawLine(x0+y,y0-x,x0+y,y0-x);
  81.         g.drawLine(x0+x,y0-y,x0+x,y0-y);
  82.         g.drawLine(x0-x,y0-y,x0-x,y0-y);
  83.         g.drawLine(x0-y,y0-x,x0-y,y0-x);
  84.         g.drawLine(x0-y,y0+x,x0-y,y0+x);
  85.         g.drawLine(x0-x,y0+y,x0-x,y0+y);
  86.     }
  87.  
  88.     // Circle is just Bresenham's algorithm for a scan converted circle
  89.     public void circle(int x0, int y0, int r, Graphics g) {
  90.         int x,y;
  91.         float d;
  92.         x=0;
  93.         y=r;
  94.         d=5/4-r;
  95.         plotpoints(x0,y0,x,y,g);
  96.  
  97.         while (y>x){
  98.             if (d<0) {
  99.                 d=d+2*x+3;
  100.                 x++;
  101.             }
  102.             else {
  103.                 d=d+2*(x-y)+5;
  104.                 x++;
  105.                 y--;
  106.             }
  107.             plotpoints(x0,y0,x,y,g);
  108.         }
  109.     }
  110.  
  111.     // Paint is the main part of the program
  112.     public void paint(Graphics g) {
  113.         int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter;
  114.         String today;
  115.  
  116.         currentDate = new Date();
  117.         SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
  118.         try {
  119.             s = Integer.parseInt(formatter.format(currentDate));
  120.         } catch (NumberFormatException n) {
  121.             s = 0;
  122.         }
  123.         formatter.applyPattern("m");
  124.         try {
  125.             m = Integer.parseInt(formatter.format(currentDate));
  126.         } catch (NumberFormatException n) {
  127.             m = 10;
  128.         }    
  129.         formatter.applyPattern("h");
  130.         try {
  131.             h = Integer.parseInt(formatter.format(currentDate));
  132.         } catch (NumberFormatException n) {
  133.             h = 10;
  134.         }
  135.         formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
  136.         today = formatter.format(currentDate);
  137.         xcenter=80;
  138.         ycenter=55;
  139.     
  140.     // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  141.     // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  142.     
  143.         xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  144.         ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  145.         xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  146.         ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  147.         xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  148.         yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  149.     
  150.     // Draw the circle and numbers
  151.     
  152.         g.setFont(clockFaceFont);
  153.         g.setColor(handColor);
  154.         circle(xcenter,ycenter,50,g);
  155.         g.setColor(numberColor);
  156.         g.drawString("9",xcenter-45,ycenter+3); 
  157.         g.drawString("3",xcenter+40,ycenter+3);
  158.         g.drawString("12",xcenter-5,ycenter-37);
  159.         g.drawString("6",xcenter-3,ycenter+45);
  160.  
  161.     // Erase if necessary, and redraw
  162.     
  163.         g.setColor(getBackground());
  164.         if (xs != lastxs || ys != lastys) {
  165.             g.drawLine(xcenter, ycenter, lastxs, lastys);
  166.             g.drawString(lastdate, 5, 125);
  167.         }
  168.         if (xm != lastxm || ym != lastym) {
  169.             g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  170.             g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  171.         if (xh != lastxh || yh != lastyh) {
  172.             g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  173.             g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  174.         g.setColor(numberColor);
  175.         g.drawString("", 5, 125);
  176.         g.drawString(today, 5, 125);    
  177.         g.drawLine(xcenter, ycenter, xs, ys);
  178.         g.setColor(handColor);
  179.         g.drawLine(xcenter, ycenter-1, xm, ym);
  180.         g.drawLine(xcenter-1, ycenter, xm, ym);
  181.         g.drawLine(xcenter, ycenter-1, xh, yh);
  182.         g.drawLine(xcenter-1, ycenter, xh, yh);
  183.         lastxs=xs; lastys=ys;
  184.         lastxm=xm; lastym=ym;
  185.         lastxh=xh; lastyh=yh;
  186.         lastdate = today;
  187.         currentDate=null;
  188.     }
  189.  
  190.     public void start() {
  191.         timer = new Thread(this);
  192.         timer.start();
  193.     }
  194.  
  195.     public void stop() {
  196.         timer = null;
  197.     }
  198.  
  199.     public void run() {
  200.         Thread me = Thread.currentThread();
  201.         while (timer == me) {
  202.             try {
  203.                 Thread.currentThread().sleep(100);
  204.             } catch (InterruptedException e) {
  205.             }
  206.             repaint();
  207.         }
  208.     }
  209.  
  210.     public void update(Graphics g) {
  211.         paint(g);
  212.     }
  213.  
  214.     public String getAppletInfo() {
  215.         return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock.";
  216.     }
  217.   
  218.     public String[][] getParameterInfo() {
  219.         String[][] info = {
  220.             {"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."},
  221.             {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue."},
  222.             {"fgcolor2", "hexadecimal RGB number", "The color of the seconds hand and numbers. Default is dark gray."}
  223.         };
  224.         return info;
  225.     }
  226. }
  227.